#include "Channels.h"
#include "ChannelManager.h"
#include "Resource.h"
#include "String.h"
#include "SharedPtr.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "NGLog.h"
#include "Character.h"
#include "CharManager.h"
#include "Items.h"
#include "ItemManager.h"
#include "WorldPacket.h"
#include "Towns.h"
#include "Effects.h"
#include "EffectManager.h"
#include "EffectModels.h"
#include "EffectModelManager.h"
#include "Users.h"
#include "UserManager.h"
#include "XMLData.h"
#include "Towns.h"
#include "Message.h"
#include "XMLData.h"
#include "OpcodesEx.h"
#include "VipCardManager.h"
#include "Tools.h"
#include "WSSocket.h"
#include "AIScript.h"

#include "twmjn/TWMJOpcode.h"
#include "twmjn/TWMJAIInterface.h"
#include "twmjn/TWMJGameStruct.h"
#include "twmjn/TWMJXmlDataManager.h"

#include "GameDefine.h"
#include LOGIC_MGR_PATH
#include PROTOCOLS_MGR_PATH
#include XMLDATA_MGR_PATH
#include LOG_MGR_PATH


namespace AIScript {
	
	
	TWMJAICreatureInterface::TWMJAICreatureInterface(Resource * creature):AICreatureBaseInterface(creature)
	{
		m_AIType = AITYPE_WCREATURE;
	}
	
	TWMJAICreatureInterface::~TWMJAICreatureInterface(void)
	{
		
	}
	
	void 	TWMJAICreatureInterface::Load(void)
	{
		chr_state = normal;
		CharPtr pSelf = ResourceProxy(m_Unit).getResourcePtr();
		sGLMgr.CombItemData(pSelf);

		m_status_endtimer = 0;
		m_bCanDelete = false;
		m_offLineTime = 0;
		initialize();
		AIInterface::Load();
	}
	
	void 	TWMJAICreatureInterface::initialize(void)
	{
		StartWaitTime = sXmlDataMgr.getTWMJconf("STARTWAITTIME");
		ResourceProxy proxy = m_Unit;
		chr = sCharMgr.getByHandle(m_Unit->getHandle());
		
		m_nLastSaveTime = now() + RandomUInt(400000,800000);
	}	
	
	void 	TWMJAICreatureInterface::Update(const uint32 & p_time)
	{
		
		if(chr.isNull())
			return ;
		
		//b_TimeOut();
		if(m_nLastSaveTime <= now())
		{
			m_nLastSaveTime = now() + RandomUInt(400000,800000);
			
			ResourceEvent event;
			event.event_id = ResourceEvent_SaveDB;
			chr->SendEvent(&event);
		}  
		uint32 curTime = time(0);
		// 检测是否可以删除
		WSSocketPtr socket = chr->getSocket();
		if(!chr->isNPC() && socket.isNull()&& GetChannelPtr().isNull())
		{
			if(!m_offLineTime)
			{
				m_offLineTime = curTime;
				m_bValidated = false;
			}
			else if(curTime - m_offLineTime >= sXmlDataMgr.getTWMJconf("USER_UNLINE_LASTT"))
			{
				if (!m_bCanDelete)
				{
					m_bCanDelete = true;
				}
			}
		}
		else
		{
			m_offLineTime = 0;
			m_bCanDelete = false;
			
		}
		AIInterface::Update(p_time);
	}	
	void TWMJAICreatureInterface::Activate(ResourcePtr & pPater)
	{
		if(pPater.isNull())
			return ;
		
		AIInterface::Activate(pPater);
	}	
	
	uint8 TWMJAICreatureInterface::get_State()
	{
		WGS_CHR_INNER_LOCK

		return chr_state;
	}

	//设置为闲置状态
	void TWMJAICreatureInterface::set_State_toNormal()
	{
		WGS_CHR_INNER_LOCK
		chr_state = normal;
	}
	
	//设置为准备状态
	void TWMJAICreatureInterface::set_State_toReady()
	{
		WGS_CHR_INNER_LOCK
		chr_state = ready;
	}

	//设置状态为游戏中	
	void TWMJAICreatureInterface::set_State_toFight()
	{
		WGS_CHR_INNER_LOCK
		chr_state = fight;
	}
	
	//设置状态为离线	
	void TWMJAICreatureInterface::set_State_toMissLink()
	{
		WGS_CHR_INNER_LOCK
		chr_state = missLink;
	}	
	//开始
	void TWMJAICreatureInterface::beginChannelWait()
	{
		set_State_toReady();
		uint32 waittime =  sGLMgr.get_acgWaitTime();
	
		if(waittime > 60)
		{
			waittime = 60;
		}
		waittime = sXmlDataMgr.getTWMJconf("STARTWAITTIME");
	//	Log.Debug("beginChannelWait","1");
		setStatusEndTimer(waittime);
	}	
	//判断
	void TWMJAICreatureInterface::b_TimeOut()
	{
		
		// ResourceProxy proxy = m_Unit;
		// CharPtr chr = sCharMgr.getByHandle(m_Unit->getHandle());
		if(chr.isNull())
			return ;
		if(chr->isNPC())
		{
			//Log.Debug("b_TimeOut","5");
			return;
		}
	//	Log.Debug("b_TimeOut","--%d",chr_state);
		if(chr_state == fight)
		{
	//		Log.Debug("b_TimeOut","4");
			return;
		}

		if(chr_state != ready)
		{
		//	Log.Debug("b_TimeOut","4");
			return;
		}
		
		if(m_status_endtimer <= now())
		{
			WorldPacket packet;
			
			uint32 waittime =  sGLMgr.get_acgWaitTime();
			
			if(waittime > 60)
			{
				waittime = 60;
			}
	//		Log.Debug("b_TimeOut","2");
	/*		waittime = sXmlDataMgr.getTWMJconf("STARTWAITTIME");
			if(sProtocolsMgr.CreateWaitTimePacket(&packet,waittime/1000))
					sGLMgr.SendProtocolsToChr(chr,&packet);
			beginChannelWait();*/
			// TWMJAIChannelInterface* pChanAIInter = TO_TWMJCHANNEL_INTERFACE(waitPtr->getAIInterface());
			// if(pChanAIInter)
			// {
				
				// pChanAIInter->leaveChannel(chr);
				// set_State_toNormal();
				
			// }
		}
	}	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}